package com.quanxi.qxaimain.controller.v1.forVueController;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.quanxi.qxaimain.common.globalException.NotControllerResponseAdvice;
import com.quanxi.qxaimain.common.leyouCors.JwtUtils;
import com.quanxi.qxaimain.dao.forVue.userMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j

@RestController

@RequestMapping("/user")
public class loginController {

    @Resource
    userMapper userMapper;
    @NotControllerResponseAdvice   //自定义返回封装 ，不走全局统一
    @PostMapping("/login")
    public Object login(    @RequestBody JSONObject user){

        Map<String, Object> map = new HashMap<>();

        String userid = user.getString("userid");
        String password = user.getString("password");

        List<Map<String, Object>> userinfo = userMapper.find_qxzsk_t_user(userid);
        if (userinfo.size()==0){
           // log.info("用户不存在");
            map.put("status", false);
            map.put("msg", "用户不存在");
            return map;
         }

        if (!userinfo.get(0).get("password").equals(password)){
        //    log.info("密码错误");
            map.put("status", false);
            map.put("msg", "密码错误");
            return map;
        }


        try {
            Map<String, String> payload = new HashMap<>();
            payload.put("password", userinfo.get(0).get("password").toString());
            payload.put("userid", userinfo.get(0).get("userid").toString());
            payload.put("username", userinfo.get(0).get("username").toString());
            payload.put("roleid", userinfo.get(0).get("roleid").toString());
            payload.put("rolename", userinfo.get(0).get("rolename").toString());
            // 生成token
            String token = JwtUtils.getToken(payload);
            map.put("code",20000);
            map.put("status", true);
            map.put("msg", "登录成功");
            map.put("token", token); // 响应token，存储在客户端

        }catch (Exception e){
         //   log.info("生成token失败...");
            map.put("status", false);
            map.put("msg", "生成token失败");
        }
        return  map;

    }


    @NotControllerResponseAdvice   //自定义返回封装 ，不走全局统一
    @GetMapping("/getInfo")
    public Map<String, Object> check(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        try {
            String token = request.getHeader("Token");
            //System.out.println(token);
            DecodedJWT verify = JwtUtils.verify(token);
            String userid = verify.getClaim("userid").asString(); // 我前面存的时候转字符串了
            String username = verify.getClaim("username").asString();
            String roleid = verify.getClaim("roleid").asString();
            String rolename = verify.getClaim("rolename").asString();
            //TODO:业务逻辑
            map.put("code",20000);
            map.put("status", true);
            map.put("userid", userid);
            map.put("roleid", roleid);
            map.put("rolename", rolename);
            map.put("username", username);
            map.put("msg", "获取用户信息成功");
            return map;
        }catch (Exception e){
            map.put("status", false);
            map.put("msg", "获取token信息失败");
            return map;
        }

    }



    @NotControllerResponseAdvice   //自定义返回封装 ，不走全局统一
    @PostMapping("/logout")
    public Object logout() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",20000);
        jsonObject.put("data" ,"success");
        return jsonObject;
    }




}
